package com.zhongyewx.teachercert.framework.okhttp;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ZYHttpParameters {

    private ArrayList<String> mKeys = new ArrayList<String>();
    private ArrayList<Object> mValues = new ArrayList<Object>();

    public ZYHttpParameters() {
    }

    public void add(String key, String value) {
        if (!TextUtils.isEmpty(key)) {
            this.mKeys.add(key);
            mValues.add(value);
        }
    }

    public void add(String key, JSONArray value) {
        if (!TextUtils.isEmpty(key) && value != null) {
            this.mKeys.add(key);
            mValues.add(value);
        }
    }

    public String getJson(ZYHttpParameters parameters) {
        String resultJson = "";
        try {

            JSONObject holder = new JSONObject();
            byte[] bytes = new byte[0];
            try {
                for (int i = 0; i < parameters.size(); i++) {
                    holder.put(parameters.getKey(i), parameters.getValue(i));
                }

                bytes = holder.toString().getBytes("GBK");
            } catch (JSONException e) {
            }
            resultJson = ZYHttpBase64.encodeToString(bytes);
        } catch (Exception ex) {
        }
        return resultJson;
    }

    public JSONObject getKaoShiJson(ZYHttpParameters parameters) {
        JSONObject holder = new JSONObject();
        try {

            try {

                for (int i = 0; i < parameters.size(); i++) {
                    holder.put(parameters.getKey(i), parameters.getValue(i));
                }
            } catch (JSONException e) {
            }
        } catch (Exception ex) {
        }
        return holder;
    }

    public String getJsonArray(ZYHttpParameters parameters) {
        String resultJson = "";
        try {
            JSONArray jsonArray = new JSONArray();
            byte[] bytes = new byte[0];
            try {
                for (int i = 0; i < parameters.size(); i++) {
                    JSONObject holder = new JSONObject();
                    holder.put(parameters.getKey(i), parameters.getValue(i));
                    jsonArray.put(holder);
                }

                bytes = jsonArray.toString().getBytes("GBK");
            } catch (JSONException e) {
            }
            resultJson = ZYHttpBase64.encodeToUrlEncodeString(bytes);
        } catch (Exception ex) {
        }
        return resultJson;
    }


    public void add(String key, int value) {
        this.mKeys.add(key);
        this.mValues.add(String.valueOf(value));
    }

    public void add(String key, long value) {
        this.mKeys.add(key);
        this.mValues.add(String.valueOf(value));
    }

    public void remove(String key) {
        int firstIndex = mKeys.indexOf(key);
        if (firstIndex >= 0) {
            this.mKeys.remove(firstIndex);
            this.mValues.remove(firstIndex);
        }

    }

    public void remove(int i) {
        if (i < mKeys.size()) {
            mKeys.remove(i);
            this.mValues.remove(i);
        }
    }

    private int getLocation(String key) {
        if (this.mKeys.contains(key)) {
            return this.mKeys.indexOf(key);
        }
        return -1;
    }

    private String getKey(int location) {
        if (location >= 0 && location < this.mKeys.size()) {
            return this.mKeys.get(location);
        }
        return "";
    }

    public String getValue(String key) {
        int index = getLocation(key);
        if (index >= 0 && index < this.mKeys.size()) {
            return (String) this.mValues.get(index);
        } else {
            return null;
        }
    }

    private Object getValue(int location) {
        if (location >= 0 && location < this.mKeys.size()) {
            Object rlt =  this.mValues.get(location);
            return rlt;
        } else {
            return null;
        }
    }

    public int size() {
        return mKeys.size();
    }

//    public void addAll(ZYHttpParameters parameters) {
//        for (int i = 0; i < parameters.size(); i++) {
//            this.add(parameters.getKey(i), parameters.getValue(i));
//        }
//    }

    public void clear() {
        this.mKeys.clear();
        this.mValues.clear();
    }
}
